<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>首页</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="../../../css/bootstrap.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="../../../js/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="../../../js/bootstrap.min.js"></script>
</head>

<body>

<form action="#" class="form-horizontal">

    <h5 class="page-header alert-info" style="padding:10px; margin:0px; margin-bottom:5px;">基本信息</h5>
	<div class="row">
    	<div class="col-xs-5">
        	<div class="form-group">
            	<label class="col-xs-3 control-label">权限编号</label>
                <div class="col-xs-9">
                	<input type="text" name="anthortyId" class="form-control input-sm" placeholder="请输入权限编号"/>
                </div>
            </div>
        
        </div>
        <div class="col-xs-5">
            <div class="form-group">
            	<label class="col-xs-3 control-label">权限名称</label>
                <div class="col-xs-9">
                	<input type="text" name="anthortyName" class="form-control input-sm" placeholder="请输入权限名称"/>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
    	<div class="col-xs-5">
        	<div class="form-group">
            	<label class="col-xs-3 control-label">上级权限</label>
                <div class="col-xs-9">
                	<select class="form-control input-sm" name="anthortyPid">

                    </select>
                </div>
            </div>
        
        </div>
        <div class="col-xs-5">
            <div class="form-group">
            	<label class="col-xs-3 control-label">权限URL</label>
                <div class="col-xs-9">
                	<input type="text" name="anthortyUrl" class="form-control input-sm" placeholder="请输入权限URL"/>
                </div>
            </div>
        </div>
    </div>
    <h5 class="page-header alert-info" style="padding:10px; margin:0px; margin-bottom:5px;">备注信息</h5>
    	<div class="row">
    	<div class="col-xs-10">
        	<div class="form-group">
            	<label class="col-xs-3 control-label">备注</label>
                <div class="col-xs-9">
                	<textarea class="form-control" name="anthortyDesc"></textarea>
                </div>
            </div>
        
        </div>

    </div>
 	<div class="row">
    	<div class="col-xs-3 col-xs-offset-3">
        	<input  type="button" class="btn btn-success" value="保存权限信息"/>
        </div>
    </div>

</form>

<script>
    $(function () {
        //扩展jquery的格式化方法
        $.fn.parseForm=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };

        $(".btn-success").click(function () {

            let param = $(".form-horizontal").parseForm();
            param.m = parent.update_anthorty ?'update':'save'
            $.post("AnthortyInfoServlet.do",param,function (data) {
                if(data == "true"){
                    $(".form-horizontal")[0].reset();//重置表单
                    initSelect();
                    parent.flushTree6(param.anthortyPid);
                }else{
                    alert("添加失败!")
                }
            })
        })


        //修改初始化表单
        var anthorty = parent.update_anthorty;
        if(anthorty){
            $("[name=anthortyId]").val(anthorty.anthortyId);
            $("[name=anthortyName]").val(anthorty.title);
            $("[name=anthortyUrl]").val(anthorty.anthortyUrl);
            $("[name=anthortyDesc]").html(anthorty.anthortyDesc);

            initSelect(anthorty.anthortyPid);
        }else{
            initSelect(1);
        }


    })


    function initSelect(pid) {
        //父节点回显
        $.post("AnthortyInfoServlet.do?m=parents",function (data) {
            $("[name=anthortyPid]")[0].length=0;
            for (let i = 0; i < data.length; i++) {
                var p = data[i];

                $("[name=anthortyPid]").append(`<option ${pid == p.anthortyId ? 'selected' : ''} value="${p.anthortyId}">${p.title}</option>`);
            }
        },"json")



    }



</script>


</body>
</html>
